<?php
$pathArr = explode("/", $_SERVER["PHP_SELF"]);
$contextPath = $pathArr[1];
$rootPath = $_SERVER["DOCUMENT_ROOT"];
include_once $rootPath . "/" . $contextPath . "/apps/ManagerIUDS.php";
$managerIUDS = new ManagerIUDS();

$mode = $_GET["mode"];
if($mode == "update"){
	$getListMedicine = $managerIUDS->queryData("*", "medicine", " WHERE medicine_id = ".$_GET["medicine_id"], "1", "=");
	echo "<input type='hidden' name='hidden_medicine_sub_type' id='hidden_medicine_sub_type' value='".$getListMedicine["medicine_sub_type"]."'>";
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>.: Medicine Detail :.</title>
<?php
require_once($rootPath . "/" . $contextPath . "/includes/css_inc.php");
require_once($rootPath . "/" . $contextPath . "/includes/js_inc.php");
?>
<script type="text/javascript">
			$(document).ready(function(){ 
				$("#save_data").click(function(){
					var result = true;
					if($("#medicine_name").val()=="" || $("#medicine_name").val()==null){
						alert("กรุณากรอกชื่อยา");
						$("#medicine_name").focus(); 
						result = false;
					}else if($("#medicine_sub_type").val()=="" || $("#medicine_sub_type").val()==null){
						alert("กรุณาเลือกประเภทของยา");
						$("#medicine_type").focus(); 
						result = false;
					}
					if(result){
						$("#frm1").submit();
					}
				});
				$("#amount").blur(function(){
					if($("#amount").val() !=$("#hidden_amount").val() && $("#amount").val() < $("#hidden_amount").val()){
						alert("ไม่สามารถลดจำนวนสิ้นค้าได้");
						$("#amount").val($("#hidden_amount").val()) ;
					}
				});
				$("#medicine_sub_type").change(function(){
					if($("#medicine_sub_type").val() != "0"){
						if($("#medicine_sub_type").val() == "S"){
							document.getElementById("price_label").innerHTML = "ราคา/เม็ด : ";
						}else if($("#medicine_sub_type").val() == "I"){
							document.getElementById("price_label").innerHTML = "ราคา/เข็ม : ";
						}else{
							document.getElementById("price_label").innerHTML = "ราคา/ขวด : ";
						}
					}else{
							document.getElementById("price_label").innerHTML = "ราคา/หน่วย : ";
					}
				});
			}); 
				function pricePreUnit(){
					//alert($("#hidden_medicine_sub_type").val());
					var valueHiddenMedicineSubType = document.getElementById("hidden_medicine_sub_type").value;
					if(valueHiddenMedicineSubType != "0"){
						if(valueHiddenMedicineSubType == "S"){
							document.getElementById("price_label").innerHTML = "ราคา/เม็ด : ";
						}else if(valueHiddenMedicineSubType == "I"){
							document.getElementById("price_label").innerHTML = "ราคา/เข็ม : ";
						}else{
							document.getElementById("price_label").innerHTML = "ราคา/ขวด : ";
						}
					}else{
							document.getElementById("price_label").innerHTML = "ราคา/หน่วย : ";
					}
				}
		</script>
</head>
<body onload="pricePreUnit()">
<center>
<form name="frm1" id="frm1" action="../actions/medicine_action.php" method="post">
<table border="0" width="75%">
	<thead class="ui-widget">
		<tr class="ui-widget-header">
			<th colspan="2" align="left">&nbsp;&nbsp;&nbsp;ลายละเอียดยา</th>
		</tr>
	</thead>
	<tbody class="ui-widget-content">
		<tr>
			<td align="right">ชื่อยา :</td>
			<td><input type="text" name="medicine_name" id="medicine_name" value="<?=$getListMedicine["medicine_name"]?>"></td>
		</tr>
		<tr>
			<td align="right">ลายละเอียดยา :</td>
			<td><textarea name="medicine_detail" id="medicine_detail" cols="21"	rows="5"><?=$getListMedicine["medicine_detail"]?></textarea></td>
		</tr><tr>
			<td align="right">ประเภท :</td>
			<td>
				<select name="medicine_sub_type" id="medicine_sub_type">
					<option value="0">--- เลือก ---</option>
					<option value="S"
					<?php
						if($getListMedicine["medicine_sub_type"] == "S"){
							echo 'selected="selected"';
						}
					?>
					>ยาเม็ด</option>
					<option value="W"
					<?php
					if($getListMedicine["medicine_sub_type"] == "W"){
							echo 'selected="selected"';
					}
					?>
					>ยาน้ำ</option>
					<option value="I"
					<?php
					if($getListMedicine["medicine_sub_type"] == "I"){
							echo 'selected="selected"';
					}
					?>
					>ยาฉีด</option>
					<option value="P"
					<?php
					if($getListMedicine["medicine_sub_type"] == "P"){
							echo 'selected="selected"';
					}
					?>
					>ยาทา</option>
				</select>
			</td>
		</tr>
		<tr>
			<td align="right"><label id="price_label">ราคา/หน่วย : </label></td>
			<td><input type="text" name="price" id="price" onkeypress="return numbersOnly(event, false);" value="<?=$getListMedicine["price"]?>"></td>
		</tr>
		<tr>
			<?php
				$label = "จำนวน";
				$readOnly = "";
				if($mode == "update"){
					$label= "จำนวนคงเหลือ";
					$readOnly = "readonly='readonly'";
				}
			?>
			<td align="right"><?=$label?>:</td>
			<td><input type="text" name="amount" id="amount" onkeypress="return numbersOnly(event, false);" <?=$readOnly?> value="<?=$getListMedicine["amount"]?>">
			</td>
		</tr>
		<?php
		if($mode == "update"){
		?>
			<tr>
			<td align="right"><label id="price_label">เพิ่มจำนวนยา:</label></td>
			<td><input type="text" name="add_amount" id="add_amount" onkeypress="return numbersOnly(event, false);" ></td>
			</tr>
		<?php
		}
		?>
		<tr>
			<td align="right">ชนิด :</td>
			<td><input type="radio" name="medicine_type" value="I"
				checked="checked">ยาใช้ภายใน <input type="radio"
				name="medicine_type" value="O"
				<?php
				if($getListMedicine["medicine_type"] == "O"){
				?>
				checked="checked"
				<?php
           	   	}
           	   	?>>ยาใช้ภายนอก</td>
		</tr>
		<tr>
			<td colspan="2" align="center">
			<input type="button" name="save_data" id="save_data" value="บันทึก">
			<input type="button" value="ยกเลิก" onclick="window.location='medicine_main.php';"></td>
		</tr>
	</tbody>
</table>
<input type="hidden" id="mode" name="mode" value=<?=$mode?>>
<input	type="hidden" id="medicine_id" name="medicine_id" value=<?=$_GET["medicine_id"]?>>
<input	type="hidden" id="hidden_amount" name="hidden_amount" value=<?=$getListMedicine["amount"]?>>
</form>
</center>
</body>
</html>
